﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using Indeco.G24.Entities;
using Indeco.G24.Data;

#endregion

namespace Indeco.G24.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="VaiTroProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class VaiTroProviderBaseCore : EntityProviderBase<Indeco.G24.Entities.VaiTro, Indeco.G24.Entities.VaiTroKey>
	{		
		#region Get from Many To Many Relationship Functions
		#region GetByIDNguoiDungFromNguoiDungVaiTro
		
		/// <summary>
		///		Gets tb_VaiTro objects from the datasource by ID_NguoiDung in the
		///		tb_NguoiDungVaiTro table. Table tb_VaiTro is related to table tb_NguoiDung
		///		through the (M:N) relationship defined in the tb_NguoiDungVaiTro table.
		/// </summary>
		/// <param name="iDNguoiDung"></param>
		/// <returns>Returns a typed collection of VaiTro objects.</returns>
		public TList<VaiTro> GetByIDNguoiDungFromNguoiDungVaiTro(System.Guid iDNguoiDung)
		{
			int count = -1;
			return GetByIDNguoiDungFromNguoiDungVaiTro(null,iDNguoiDung, 0, int.MaxValue, out count);
			
		}
		
		/// <summary>
		///		Gets Indeco.G24.Entities.VaiTro objects from the datasource by ID_NguoiDung in the
		///		tb_NguoiDungVaiTro table. Table tb_VaiTro is related to table tb_NguoiDung
		///		through the (M:N) relationship defined in the tb_NguoiDungVaiTro table.
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="iDNguoiDung"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of VaiTro objects.</returns>
		public TList<VaiTro> GetByIDNguoiDungFromNguoiDungVaiTro(System.Guid iDNguoiDung, int start, int pageLength)
		{
			int count = -1;
			return GetByIDNguoiDungFromNguoiDungVaiTro(null, iDNguoiDung, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets VaiTro objects from the datasource by ID_NguoiDung in the
		///		tb_NguoiDungVaiTro table. Table tb_VaiTro is related to table tb_NguoiDung
		///		through the (M:N) relationship defined in the tb_NguoiDungVaiTro table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="iDNguoiDung"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of tb_VaiTro objects.</returns>
		public TList<VaiTro> GetByIDNguoiDungFromNguoiDungVaiTro(TransactionManager transactionManager, System.Guid iDNguoiDung)
		{
			int count = -1;
			return GetByIDNguoiDungFromNguoiDungVaiTro(transactionManager, iDNguoiDung, 0, int.MaxValue, out count);
		}
		
		
		/// <summary>
		///		Gets VaiTro objects from the datasource by ID_NguoiDung in the
		///		tb_NguoiDungVaiTro table. Table tb_VaiTro is related to table tb_NguoiDung
		///		through the (M:N) relationship defined in the tb_NguoiDungVaiTro table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="iDNguoiDung"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of tb_VaiTro objects.</returns>
		public TList<VaiTro> GetByIDNguoiDungFromNguoiDungVaiTro(TransactionManager transactionManager, System.Guid iDNguoiDung,int start, int pageLength)
		{
			int count = -1;
			return GetByIDNguoiDungFromNguoiDungVaiTro(transactionManager, iDNguoiDung, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets VaiTro objects from the datasource by ID_NguoiDung in the
		///		tb_NguoiDungVaiTro table. Table tb_VaiTro is related to table tb_NguoiDung
		///		through the (M:N) relationship defined in the tb_NguoiDungVaiTro table.
		/// </summary>
		/// <param name="iDNguoiDung"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of tb_VaiTro objects.</returns>
		public TList<VaiTro> GetByIDNguoiDungFromNguoiDungVaiTro(System.Guid iDNguoiDung,int start, int pageLength, out int count)
		{
			
			return GetByIDNguoiDungFromNguoiDungVaiTro(null, iDNguoiDung, start, pageLength, out count);
		}


		/// <summary>
		///		Gets tb_VaiTro objects from the datasource by ID_NguoiDung in the
		///		tb_NguoiDungVaiTro table. Table tb_VaiTro is related to table tb_NguoiDung
		///		through the (M:N) relationship defined in the tb_NguoiDungVaiTro table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <param name="iDNguoiDung"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of VaiTro objects.</returns>
		public abstract TList<VaiTro> GetByIDNguoiDungFromNguoiDungVaiTro(TransactionManager transactionManager,System.Guid iDNguoiDung, int start, int pageLength, out int count);
		
		#endregion GetByIDNguoiDungFromNguoiDungVaiTro
		
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, Indeco.G24.Entities.VaiTroKey key)
		{
			return Delete(transactionManager, key.IDVaiTro);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="iDVaiTro">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.Guid iDVaiTro)
		{
			return Delete(null, iDVaiTro);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="iDVaiTro">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.Guid iDVaiTro);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override Indeco.G24.Entities.VaiTro Get(TransactionManager transactionManager, Indeco.G24.Entities.VaiTroKey key, int start, int pageLength)
		{
			return GetByIDVaiTro(transactionManager, key.IDVaiTro, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_tb_VaiTro index.
		/// </summary>
		/// <param name="iDVaiTro"></param>
		/// <returns>Returns an instance of the <see cref="Indeco.G24.Entities.VaiTro"/> class.</returns>
		public Indeco.G24.Entities.VaiTro GetByIDVaiTro(System.Guid iDVaiTro)
		{
			int count = -1;
			return GetByIDVaiTro(null,iDVaiTro, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_tb_VaiTro index.
		/// </summary>
		/// <param name="iDVaiTro"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Indeco.G24.Entities.VaiTro"/> class.</returns>
		public Indeco.G24.Entities.VaiTro GetByIDVaiTro(System.Guid iDVaiTro, int start, int pageLength)
		{
			int count = -1;
			return GetByIDVaiTro(null, iDVaiTro, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_tb_VaiTro index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="iDVaiTro"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Indeco.G24.Entities.VaiTro"/> class.</returns>
		public Indeco.G24.Entities.VaiTro GetByIDVaiTro(TransactionManager transactionManager, System.Guid iDVaiTro)
		{
			int count = -1;
			return GetByIDVaiTro(transactionManager, iDVaiTro, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_tb_VaiTro index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="iDVaiTro"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Indeco.G24.Entities.VaiTro"/> class.</returns>
		public Indeco.G24.Entities.VaiTro GetByIDVaiTro(TransactionManager transactionManager, System.Guid iDVaiTro, int start, int pageLength)
		{
			int count = -1;
			return GetByIDVaiTro(transactionManager, iDVaiTro, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_tb_VaiTro index.
		/// </summary>
		/// <param name="iDVaiTro"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Indeco.G24.Entities.VaiTro"/> class.</returns>
		public Indeco.G24.Entities.VaiTro GetByIDVaiTro(System.Guid iDVaiTro, int start, int pageLength, out int count)
		{
			return GetByIDVaiTro(null, iDVaiTro, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_tb_VaiTro index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="iDVaiTro"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="Indeco.G24.Entities.VaiTro"/> class.</returns>
		public abstract Indeco.G24.Entities.VaiTro GetByIDVaiTro(TransactionManager transactionManager, System.Guid iDVaiTro, int start, int pageLength, out int count);
						
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key IX_tb_VaiTro index.
		/// </summary>
		/// <param name="tenVaiTro"></param>
		/// <returns>Returns an instance of the <see cref="Indeco.G24.Entities.VaiTro"/> class.</returns>
		public Indeco.G24.Entities.VaiTro GetByTenVaiTro(System.String tenVaiTro)
		{
			int count = -1;
			return GetByTenVaiTro(null,tenVaiTro, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the IX_tb_VaiTro index.
		/// </summary>
		/// <param name="tenVaiTro"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Indeco.G24.Entities.VaiTro"/> class.</returns>
		public Indeco.G24.Entities.VaiTro GetByTenVaiTro(System.String tenVaiTro, int start, int pageLength)
		{
			int count = -1;
			return GetByTenVaiTro(null, tenVaiTro, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the IX_tb_VaiTro index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="tenVaiTro"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Indeco.G24.Entities.VaiTro"/> class.</returns>
		public Indeco.G24.Entities.VaiTro GetByTenVaiTro(TransactionManager transactionManager, System.String tenVaiTro)
		{
			int count = -1;
			return GetByTenVaiTro(transactionManager, tenVaiTro, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the IX_tb_VaiTro index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="tenVaiTro"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Indeco.G24.Entities.VaiTro"/> class.</returns>
		public Indeco.G24.Entities.VaiTro GetByTenVaiTro(TransactionManager transactionManager, System.String tenVaiTro, int start, int pageLength)
		{
			int count = -1;
			return GetByTenVaiTro(transactionManager, tenVaiTro, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the IX_tb_VaiTro index.
		/// </summary>
		/// <param name="tenVaiTro"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Indeco.G24.Entities.VaiTro"/> class.</returns>
		public Indeco.G24.Entities.VaiTro GetByTenVaiTro(System.String tenVaiTro, int start, int pageLength, out int count)
		{
			return GetByTenVaiTro(null, tenVaiTro, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the IX_tb_VaiTro index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="tenVaiTro"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="Indeco.G24.Entities.VaiTro"/> class.</returns>
		public abstract Indeco.G24.Entities.VaiTro GetByTenVaiTro(TransactionManager transactionManager, System.String tenVaiTro, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a Indeco.G24.Entities.TList&lt;VaiTro&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="Indeco.G24.Entities.TList&lt;VaiTro&gt;"/></returns>
		public static Indeco.G24.Entities.TList<VaiTro> Fill(IDataReader reader, Indeco.G24.Entities.TList<VaiTro> rows, int start, int pageLength)
		{
		// advance to the starting row
		for (int i = 0; i < start; i++)
		{
			if (!reader.Read())
			return rows; // not enough rows, just return
		}
		for (int i = 0; i < pageLength; i++)
		{
			if (!reader.Read())
			break; // we are done
			string key = null;
			
			Indeco.G24.Entities.VaiTro c = null;
			if (DataRepository.Provider.UseEntityFactory)
			{
			key = new System.Text.StringBuilder("VaiTro")
			.Append("|").Append((reader.IsDBNull(((int)Indeco.G24.Entities.VaiTroColumn.IDVaiTro - 1))?Guid.NewGuid():(System.Guid)reader[((int)Indeco.G24.Entities.VaiTroColumn.IDVaiTro - 1)]).ToString()).ToString();
			c = EntityManager.LocateOrCreate<VaiTro>(
			key.ToString(), // EntityTrackingKey
			"VaiTro",  //Creational Type
			DataRepository.Provider.EntityCreationalFactoryType,  //Factory used to create entity
			DataRepository.Provider.EnableEntityTracking); // Track this entity?
			}
			else
			{
			c = new Indeco.G24.Entities.VaiTro();
			}
			
			if (!DataRepository.Provider.EnableEntityTracking ||
			c.EntityState == EntityState.Added ||
			(DataRepository.Provider.EnableEntityTracking &&
			((DataRepository.Provider.CurrentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
			(DataRepository.Provider.CurrentLoadPolicy == LoadPolicy.DiscardChanges && (c.EntityState == EntityState.Unchanged ||
								c.EntityState == EntityState.Changed)))))
						{
			c.SuppressEntityEvents = true;
			c.IDVaiTro = (System.Guid)reader["ID_VaiTro"];
			c.OriginalIDVaiTro = c.IDVaiTro;
			c.TenVaiTro = (System.String)reader["TenVaiTro"];
			c.MoTa = reader.IsDBNull(reader.GetOrdinal("MoTa")) ? null : (System.String)reader["MoTa"];
			c.Xem = (System.Boolean)reader["Xem"];
			c.Sua = (System.Boolean)reader["Sua"];
			c.Xoa = (System.Boolean)reader["Xoa"];
			c.Taomoi = (System.Boolean)reader["Taomoi"];
			c.EntityTrackingKey = key;
			c.AcceptChanges();
			c.SuppressEntityEvents = false;
			}
			rows.Add(c);
		}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="Indeco.G24.Entities.VaiTro"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="Indeco.G24.Entities.VaiTro"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, Indeco.G24.Entities.VaiTro entity)
		{
			if (!reader.Read()) return;
			
			entity.IDVaiTro = (System.Guid)reader["ID_VaiTro"];
			entity.OriginalIDVaiTro = (System.Guid)reader["ID_VaiTro"];
			entity.TenVaiTro = (System.String)reader["TenVaiTro"];
			entity.MoTa = reader.IsDBNull(reader.GetOrdinal("MoTa")) ? null : (System.String)reader["MoTa"];
			entity.Xem = (System.Boolean)reader["Xem"];
			entity.Sua = (System.Boolean)reader["Sua"];
			entity.Xoa = (System.Boolean)reader["Xoa"];
			entity.Taomoi = (System.Boolean)reader["Taomoi"];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="Indeco.G24.Entities.VaiTro"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="Indeco.G24.Entities.VaiTro"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, Indeco.G24.Entities.VaiTro entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.IDVaiTro = (System.Guid)dataRow["ID_VaiTro"];
			entity.OriginalIDVaiTro = (System.Guid)dataRow["ID_VaiTro"];
			entity.TenVaiTro = (System.String)dataRow["TenVaiTro"];
			entity.MoTa = Convert.IsDBNull(dataRow["MoTa"]) ? null : (System.String)dataRow["MoTa"];
			entity.Xem = (System.Boolean)dataRow["Xem"];
			entity.Sua = (System.Boolean)dataRow["Sua"];
			entity.Xoa = (System.Boolean)dataRow["Xoa"];
			entity.Taomoi = (System.Boolean)dataRow["Taomoi"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="Indeco.G24.Entities.VaiTro"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">Indeco.G24.Entities.VaiTro Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		internal override void DeepLoad(TransactionManager transactionManager, Indeco.G24.Entities.VaiTro entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			// Deep load child collections  - Call GetByIDVaiTro methods when available
			
			#region NguoiDungVaiTroCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<NguoiDungVaiTro>|NguoiDungVaiTroCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'NguoiDungVaiTroCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.NguoiDungVaiTroCollection = DataRepository.NguoiDungVaiTroProvider.GetByIDVaiTro(transactionManager, entity.IDVaiTro);

				if (deep && entity.NguoiDungVaiTroCollection.Count > 0)
				{
					deepHandles.Add("NguoiDungVaiTroCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<NguoiDungVaiTro>) DataRepository.NguoiDungVaiTroProvider.DeepLoad,
						new object[] { transactionManager, entity.NguoiDungVaiTroCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			#region NguoiDungCollection_From_NguoiDungVaiTro
			// RelationshipType.ManyToMany
			if (CanDeepLoad(entity, "List<NguoiDung>|NguoiDungCollection_From_NguoiDungVaiTro", deepLoadType, innerList))
			{
				entity.NguoiDungCollection_From_NguoiDungVaiTro = DataRepository.NguoiDungProvider.GetByIDVaiTroFromNguoiDungVaiTro(transactionManager, entity.IDVaiTro);			 
		
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'NguoiDungCollection_From_NguoiDungVaiTro' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.NguoiDungCollection_From_NguoiDungVaiTro != null)
				{
					deepHandles.Add("NguoiDungCollection_From_NguoiDungVaiTro",
						new KeyValuePair<Delegate, object>((DeepLoadHandle< NguoiDung >) DataRepository.NguoiDungProvider.DeepLoad,
						new object[] { transactionManager, entity.NguoiDungCollection_From_NguoiDungVaiTro, deep, deepLoadType, childTypes, innerList }
					));
				}
			}
			#endregion
			
			
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the Indeco.G24.Entities.VaiTro object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">Indeco.G24.Entities.VaiTro instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">Indeco.G24.Entities.VaiTro Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		internal override bool DeepSave(TransactionManager transactionManager, Indeco.G24.Entities.VaiTro entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<Delegate, object> deepHandles = new Dictionary<Delegate, object>();

			#region NguoiDungCollection_From_NguoiDungVaiTro>
			if (CanDeepSave(entity.NguoiDungCollection_From_NguoiDungVaiTro, "List<NguoiDung>|NguoiDungCollection_From_NguoiDungVaiTro", deepSaveType, innerList))
			{
				if (entity.NguoiDungCollection_From_NguoiDungVaiTro.Count > 0 || entity.NguoiDungCollection_From_NguoiDungVaiTro.DeletedItems.Count > 0)
				{
					DataRepository.NguoiDungProvider.Save(transactionManager, entity.NguoiDungCollection_From_NguoiDungVaiTro); 
					deepHandles.Add(
						(DeepSaveHandle<NguoiDung>) DataRepository.NguoiDungProvider.DeepSave,
						new object[] { transactionManager, entity.NguoiDungCollection_From_NguoiDungVaiTro, deepSaveType, childTypes, innerList }
					);
				}
			}
			#endregion 
	
			#region List<NguoiDungVaiTro>
				if (CanDeepSave(entity.NguoiDungVaiTroCollection, "List<NguoiDungVaiTro>|NguoiDungVaiTroCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(NguoiDungVaiTro child in entity.NguoiDungVaiTroCollection)
					{
						if(child.IDVaiTroSource != null)
							child.IDVaiTro = child.IDVaiTroSource.IDVaiTro;
						else
							child.IDVaiTro = entity.IDVaiTro;

						//Handle right table of NguoiDungCollection_From_NguoiDungVaiTro
						if(child.IDVaiTroSource != null)
						{
								child.IDVaiTro = child.IDVaiTroSource.IDVaiTro;

						}

						//Handle right table of NguoiDungCollection_From_NguoiDungVaiTro
						if(child.IDNguoiDungSource != null)
						{
								child.IDNguoiDung = child.IDNguoiDungSource.IDNguoiDung;

						}

					}

					if (entity.NguoiDungVaiTroCollection.Count > 0 || entity.NguoiDungVaiTroCollection.DeletedItems.Count > 0)
					{
						DataRepository.NguoiDungVaiTroProvider.Save(transactionManager, entity.NguoiDungVaiTroCollection);
						
						deepHandles.Add(
							(DeepSaveHandle< NguoiDungVaiTro >) DataRepository.NguoiDungVaiTroProvider.DeepSave,
							new object[] { transactionManager, entity.NguoiDungVaiTroCollection, deepSaveType, childTypes, innerList }
						);
					}
				} 
			#endregion 
				
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region VaiTroChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>Indeco.G24.Entities.VaiTro</c>
	///</summary>
	public enum VaiTroChildEntityTypes
	{

		///<summary>
		/// Collection of <c>VaiTro</c> as OneToMany for NguoiDungVaiTroCollection
		///</summary>
		[ChildEntityType(typeof(TList<NguoiDungVaiTro>))]
		NguoiDungVaiTroCollection,

		///<summary>
		/// Collection of <c>VaiTro</c> as ManyToMany for NguoiDungCollection_From_NguoiDungVaiTro
		///</summary>
		[ChildEntityType(typeof(TList<NguoiDung>))]
		NguoiDungCollection_From_NguoiDungVaiTro,
	}
	
	#endregion VaiTroChildEntityTypes
	
	#region VaiTroFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;VaiTroColumn&gt;"/> class
	/// that is used exclusively with a <see cref="VaiTro"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class VaiTroFilterBuilder : SqlFilterBuilder<VaiTroColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the VaiTroFilterBuilder class.
		/// </summary>
		public VaiTroFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the VaiTroFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public VaiTroFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the VaiTroFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public VaiTroFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion VaiTroFilterBuilder
	
	#region VaiTroParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;VaiTroColumn&gt;"/> class
	/// that is used exclusively with a <see cref="VaiTro"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class VaiTroParameterBuilder : ParameterizedSqlFilterBuilder<VaiTroColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the VaiTroParameterBuilder class.
		/// </summary>
		public VaiTroParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the VaiTroParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public VaiTroParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the VaiTroParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public VaiTroParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion VaiTroParameterBuilder
} // end namespace
